<template>
    <div class="register" v-if="store.isRegister">
        <el-icon style="color:block;float: right;cursor: pointer;" :size="24" @click="close">
            <Close></Close>
        </el-icon>
        <h3>用户注册</h3>

        <div class="username">
            <input type="text" placeholder="请输入用户名" v-model="registerInfo.username">
            <p>{{text1}}</p>
        </div>
        <div class="password">
            <input :type="isPwd1 ? 'password' : 'text'" placeholder="请输入密码" v-model="registerInfo.password">
            <p>{{text2}}</p>
            <div class="icon_box">
            <el-icon @click="isPwd1 = !isPwd1" v-if="isPwd1"><View /></el-icon>
            <el-icon @click="isPwd1 = !isPwd1" v-else><Hide /></el-icon>
            </div>
        </div>
        <div class="password1">
            <input :type="isPwd2 ? 'password' : 'text'" placeholder="请再次输入密码" v-model="registerInfo.password1">
            <p>{{text4}}</p>
            <div class="icon_box">
            <el-icon @click="isPwd2 = !isPwd2" v-if="isPwd2"><View /></el-icon>
            <el-icon @click="isPwd2 = !isPwd2" v-else><Hide /></el-icon>
            </div>
        </div>
        <div class="code">
            <div class="code1">
                <input type="text" placeholder="请输入验证码" v-model="registerInfo.img_code">
                <img :src="dataImg" @click="getImgData">
            </div>
            <p>{{text3}}</p>
        </div>
        <div class="btn">
            <button @click="register">注册</button>
        </div>
        <div class="text">
            <a @click="goLogin"> 登录 </a> <span> 已有账号? </span> 
        </div>
    </div>
</template>

<script lang='ts' setup>
import { onMounted, reactive, ref } from 'vue'
import { getCodeApi } from '@/api/front'
import {registerApi} from '@/api/front/index'
import mainStore from '@/stores'
const store = mainStore()
let dataImg = ref()
let isPwd1 = ref(true)
let isPwd2 = ref(true)

let registerInfo = reactive({
    username: '',
    password: '',
    img_code: '',
    password1:''
})
//提示文字
let text1 = ref()
let text2 = ref()
let text3 = ref()
let text4 = ref()
//或者验证码base64
let getImgData = async () => {
    let data = await getCodeApi()
    dataImg.value = data
}
//注册
let register = () => {
    text1.value = ''
    text2.value = ''
    text3.value = ''
    text4.value = ''
    let { username, password, img_code,password1 } = registerInfo
    if (!username) {
        text1.value = '用户名不能为空!'
        return
    }

    let reg = /^[a-zA-Z]{1}[0-9A-Za-z]{4,19}/
    if(!reg.test(username)){
        text1.value = '只能以字符串开头且长度在5-20!'
        return
    }
    if (!password) {
        text2.value = '密码不能为空!'
        return
    }
    if( password.length < 6){
        text2.value = '密码长度不能小于6!'
    }
    if(password1 !== password){
        text4.value = '两次密码不一致!'
    } 
    if (!img_code) {
        text3.value = '验证码不能为空!'
        return
    }
    registerApi({username, password, img_code}).then((res:any)=>{
        if(res.err_msg){
            text3.value = res.err_msg
            return 
        }
        if(res.code == 200){
            text3.value = '注册成功!'
            store.userInfo.username = username
            store.userInfo.password = password
            setTimeout(()=>{
                goLogin()
            },500)
        }
    })
}
//切换到登录
let goLogin = ()=>{
    store.isRegister = false
    store.isLogin = true
}
//关闭
let close = ()=>{
    store.isRegister = false
    store.isLogin = false
}
onMounted(() => {
    getImgData()
})
</script>

<style lang='scss' scoped>
.register {
    width: 320px;
    padding: 25px;
    background-color: #fff;
    box-shadow: 0px 0px 10px 0px rgb(0 0 0 / 18%);
    position: absolute;
    top: 200px;
    transform: translateX(-50%);
    left: 50%;
    h3{
        text-align: center;
        margin-bottom: 30px;
    }
    .username,
    .password,
    .password1 {
        box-sizing: border-box;
        position: relative;
        input {
            width: 300px;
        }
        .icon_box{
            right: 5px;
            top: 9px;
            position: absolute;
            color: #666;
            i{
                font-size: 19px;
                cursor: pointer;
            }
        }
    }

    .code1 {
        display: flex;
        align-items: center;
        justify-content: space-between;
    }

    input {
        padding: 10px 10px;
        border-radius: 5px;
        border: 1px solid #eee;
    }

    input:hover {
        border-color: red;
    }

    img {
        height: 40px;
        width: 100px;
        cursor: pointer;
        border: 1px solid #eee;
    }

    .btn {
        button {
            width: 100%;
            height: 40px;
            margin-right: 15px;
            color: white;
            background-color: #1abc9c;
            border-radius: 3px;
            border: 1px solid #1abc9c;
            box-sizing: border-box;
            cursor: pointer;
        }
    }

    .username,
    .password,
    .btn,
    .password1,
    .code {
        margin-bottom: 10px;
        p{
            font-size: 12px;
            text-indent: 5px;
            margin-top: 5px;
            height: 16px;
            color: red;
        }
    }
    .text{
        height: 20px;
        font-size: 13px;
        a{
            float: right;
            color: red;
            cursor: pointer;
            margin-left: 5px;
            user-select: none;
        }
        span{
            float: right;
        }
    }
}
</style>